package ptij.ADSL.pn.ATUR;

import ptolemy.kernel.util.*;
import ptolemy.actor.*;
import ptolemy.actor.gui.*;
import ptolemy.data.expr.*;
import ptolemy.data.*;
import ptolemy.domains.pn.kernel.*;

public class ATUR extends TypedCompositeActor {

    /* Input Ports */
    public TypedIOPort rx;
    public TypedIOPort dataTx;
    public TypedIOPort LS0_in;

    /* Output Ports */
    public TypedIOPort LS0_out;
    public TypedIOPort AS0;
    public TypedIOPort tx; 
    public TypedIOPort dataRx;

    public static final int AS0Bytes_C = 10;
    public static final boolean AS0Map_C = false;
    public static final int LS0Bytes_C = 10;
    public static final boolean LS0Map_C = false;
    public static final int S_C = 1;
    public static final int parityFast_C = 2;
    public static final int parityInter_C = 0;
    public static final int D_C = 1;

    public static final int LS0Bytes_R = 5;
    public static final boolean LS0Map_R = true;
    public static final int S_R = 2;
    public static final int parityFast_R = 2;
    public static final int parityInter_R = 2;
    public static final int D_R = 2;

    public static final int framesPerSuperframe = 6;
        
    public ATUR(TypedCompositeActor container, String name) throws
        NameDuplicationException, IllegalActionException {
        
	super(container, name);
	
	rx = new TypedIOPort(this, "rx", true, false);
	rx.setTypeEquals(DoubleToken.class);
	
	dataTx = new TypedIOPort(this, "dataTx", true, false);
	dataTx.setTypeEquals(IntToken.class);

	LS0_in = new TypedIOPort(this, "LS0_in", true, false);
	LS0_in.setTypeEquals(ObjectToken.class);

	LS0_out = new TypedIOPort(this, "LS0_out", false, true);
	LS0_out.setTypeEquals(ObjectToken.class);

	AS0 = new TypedIOPort(this, "AS0", false, true);
	AS0.setTypeEquals(ObjectToken.class);
		
	tx = new TypedIOPort(this, "tx", false, true);
	tx.setTypeEquals(DoubleToken.class);

	dataRx = new TypedIOPort(this, "dataRx", false, true);
	dataRx.setTypeEquals(IntToken.class);

        /* TX */
        _muxtx = new MuxSyncTx(this, "MuxSyncTx");
        _muxtx.LS0Bytes.setToken(new IntToken(LS0Bytes_R));
	_muxtx.LS0Map.setToken(new BooleanToken(LS0Map_R));
	_muxtx.S.setToken(new IntToken(S_R));

	_crcTx = new CRCTx(this, "crcTx");
	_crcTx.LS0Bytes.setToken(new IntToken(LS0Bytes_R));
	_crcTx.LS0Map.setToken(new BooleanToken(LS0Map_R));
	_crcTx.S.setToken(new IntToken(S_R));

	_scramblerTx = new ScramblerTx(this, "scramblerTx");
	_scramblerTx.LS0Bytes.setToken(new IntToken(LS0Bytes_R));
	_scramblerTx.LS0Map.setToken(new BooleanToken(LS0Map_R));
	_scramblerTx.S.setToken(new IntToken(S_R));
	
        _fectx = new FECTx(this, "fectx");
	_fectx.LS0Bytes.setToken(new IntToken(LS0Bytes_R));
	_fectx.LS0Map.setToken(new BooleanToken(LS0Map_R));
	_fectx.S.setToken(new IntToken(S_R));
	_fectx.parityFast.setToken(new IntToken(parityFast_R));
	_fectx.parityInter.setToken(new IntToken(parityInter_R));

	_interTx = new InterleaverTx(this, "interTx");
	_interTx.LS0Bytes.setToken(new IntToken(LS0Bytes_R));
	_interTx.LS0Map.setToken(new BooleanToken(LS0Map_R));
	_interTx.S.setToken(new IntToken(S_R));
	_interTx.parityInter.setToken(new IntToken(parityInter_R));
	_interTx.D.setToken(new IntToken(D_R));

	_map = new Map(this, "Map");
	_map.LS0Bytes.setToken(new IntToken(LS0Bytes_R));
	_map.LS0Map.setToken(new BooleanToken(LS0Map_R));
	_map.S.setToken(new IntToken(S_R));
	_map.parityFast.setToken(new IntToken(parityFast_R));
	_map.parityInter.setToken(new IntToken(parityInter_R));

        _idft = new IDFT(this, "IDFT");

        /* RX */
	_dft = new DFT(this, "DFT");

	_demap = new Demap(this, "Demap");
	_demap.AS0Bytes.setToken(new IntToken(AS0Bytes_C));
	_demap.LS0Bytes.setToken(new IntToken(LS0Bytes_C));
	_demap.AS0Map.setToken(new BooleanToken(AS0Map_C));
	_demap.LS0Map.setToken(new BooleanToken(LS0Map_C));
	_demap.S.setToken(new IntToken(S_C));
        _demap.parityFast.setToken(new IntToken(parityFast_C));
	_demap.parityInter.setToken(new IntToken(parityInter_C));

	_interRx = new InterleaverRx(this, "interRx");
	_interRx.AS0Bytes.setToken(new IntToken(AS0Bytes_C));
	_interRx.LS0Bytes.setToken(new IntToken(LS0Bytes_C));
	_interRx.AS0Map.setToken(new BooleanToken(AS0Map_C));
	_interRx.LS0Map.setToken(new BooleanToken(LS0Map_C));
	_interRx.S.setToken(new IntToken(S_C));
	_interRx.parityInter.setToken(new IntToken(parityInter_C));
	_interRx.D.setToken(new IntToken(D_C));
	    	    
        _fecrx = new FECRx(this, "FECRx");
	_fecrx.AS0Bytes.setToken(new IntToken(AS0Bytes_C));
	_fecrx.LS0Bytes.setToken(new IntToken(LS0Bytes_C));
	_fecrx.AS0Map.setToken(new BooleanToken(AS0Map_C));
	_fecrx.LS0Map.setToken(new BooleanToken(LS0Map_C));
	_fecrx.S.setToken(new IntToken(S_C));
	_fecrx.parityFast.setToken(new IntToken(parityFast_C));
	_fecrx.parityInter.setToken(new IntToken(parityInter_C));

	_scramblerRx = new ScramblerRx(this, "scramblerRx");
	_scramblerRx.AS0Bytes.setToken(new IntToken(AS0Bytes_C));
	_scramblerRx.LS0Bytes.setToken(new IntToken(LS0Bytes_C));
	_scramblerRx.AS0Map.setToken(new BooleanToken(AS0Map_C));
	_scramblerRx.LS0Map.setToken(new BooleanToken(LS0Map_C));
	_scramblerRx.S.setToken(new IntToken(S_C));

        _crcRx = new CRCRx(this, "crcRx");
	_crcRx.AS0Bytes.setToken(new IntToken(AS0Bytes_C));
        _crcRx.LS0Bytes.setToken(new IntToken(LS0Bytes_C));
	_crcRx.AS0Map.setToken(new BooleanToken(AS0Map_C));
	_crcRx.LS0Map.setToken(new BooleanToken(LS0Map_C));
	_crcRx.S.setToken(new IntToken(S_C));

	_muxrx = new MuxSyncRx(this, "MuxSyncRx");
	_muxrx.S.setToken(new IntToken(S_C));

	/* Tx Path */
	connect(LS0_in, _muxtx.LS0);
	connect(dataTx, _muxtx.dataTx);
	connect(_muxtx.output, _crcTx.input);
	connect(_crcTx.output, _scramblerTx.input);	
	connect(_scramblerTx.output, _fectx.input);
	connect(_fectx.fast, _map.fast);
	connect(_fectx.inter, _interTx.input);
	connect(_interTx.output, _map.inter);

	for(int i = 0;i < 31;i++)
	    connect(_map.subChannel[i], _idft.subChannel[i]);
	    
	connect(_idft.output, tx);
	
	/* RX Path */
	connect(rx, _dft.input);
	for(int i = 0;i < 255;i++)
	    connect(_dft.subChannel[i], _demap.subChannel[i]);
			    		
	connect(_demap.inter, _interRx.input);
	connect(_interRx.output, _fecrx.inter);
	connect(_demap.fast, _fecrx.fast);
	connect(_fecrx.output, _scramblerRx.input);
        connect(_scramblerRx.output, _crcRx.input);
        connect(_crcRx.output, _muxrx.input);
	connect(_muxrx.AS0, AS0);
	connect(_muxrx.LS0, LS0_out);
	connect(_muxrx.dataRx, dataRx);
    }
    private MuxSyncTx _muxtx;
    private CRCTx _crcTx;
    private ScramblerTx _scramblerTx;
    private FECTx _fectx;
    private InterleaverTx _interTx;
    private Map _map;
    private IDFT _idft;
    
    private DFT _dft;
    private Demap _demap;
    private InterleaverRx _interRx;
    private FECRx _fecrx;
    private ScramblerRx _scramblerRx;
    private CRCRx _crcRx;
    private MuxSyncRx _muxrx;
}
